package com.chuangjiangx.member.stored.ddd.query;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.domain.shared.model.PayEntry;
import com.chuangjiangx.domain.shared.model.PayTerminal;
import com.chuangjiangx.member.stored.ddd.dal.condition.AvailableBalanceCondition;
import com.chuangjiangx.member.stored.ddd.dal.condition.QueryClientStoredListCondition;
import com.chuangjiangx.member.stored.ddd.dal.condition.QueryStoredListForClientCondition;
import com.chuangjiangx.member.stored.ddd.dal.mapper.StoredStreamDalMapper;
import com.chuangjiangx.member.stored.ddd.dal.dto.AvailableBalance;
import com.chuangjiangx.member.stored.ddd.dal.dto.ClientStoredStreamList;
import com.chuangjiangx.member.stored.ddd.dal.dto.StoredStreamInfoForClient;
import com.chuangjiangx.member.stored.ddd.dal.dto.StoredStreamListForClient;
import com.chuangjiangx.member.stored.ddd.query.dto.ClientStoredStreamListDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamInfoForClientDTO;
import com.chuangjiangx.member.stored.ddd.query.dto.StoredStreamListForClientDTO;
import com.chuangjiangx.member.basic.ddd.domain.model.MbrSourceTerminal;
import com.chuangjiangx.member.stored.ddd.domain.model.MbrStoredType;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * 储值明细相关查询 --客户端
 * Created by YangKuo on 2017/11/10.
 */
@Component
public class MbrStoredStreamClientQuery {


    private final StoredStreamDalMapper storedStreamDalMapper;

    @Autowired
    public MbrStoredStreamClientQuery(StoredStreamDalMapper storedStreamDalMapper) {
        this.storedStreamDalMapper = storedStreamDalMapper;
    }

    /**
     * 查询储值明细列表-客户端
     */
    public PagingResult<StoredStreamListForClientDTO> searchStoredStreamListForClient(QueryStoredListForClientCondition condition) {

        Assert.notNull(condition.getMerchantId(), "商户id不能为空！");
        RowBounds rowBounds = new RowBounds(condition.offset(), condition.limit());
        List<StoredStreamListForClient> list = storedStreamDalMapper.queryStoredStreamListForClient(condition, rowBounds);

        PageInfo<StoredStreamListForClient> pageInfo = new PageInfo<>(list);

        List<StoredStreamListForClientDTO> detailListDTOs = new ArrayList<>();

        list.forEach(storedStreamListForClient -> {
            StoredStreamListForClientDTO listForClientDTO = new StoredStreamListForClientDTO(
                    storedStreamListForClient.getId(),
                    storedStreamListForClient.getStoredNum(),
                    storedStreamListForClient.getDateTime(),
                    storedStreamListForClient.getMemberCardNum(),
                    storedStreamListForClient.getMobile(),
                    storedStreamListForClient.getType(),
                    storedStreamListForClient.getTransactionAmount(),
                    storedStreamListForClient.getGiftAmount(),
                    storedStreamListForClient.getAvailableBalance(),
                    storedStreamListForClient.getPayTerminal()
            );


            AvailableBalanceCondition con = new AvailableBalanceCondition();
            con.setRefundNum(storedStreamListForClient.getOrderRefundNum());
            con.setOrderNum(storedStreamListForClient.getStoredNum());
            con.setType(storedStreamListForClient.getType());

            AvailableBalance balance = storedStreamDalMapper.queryByOrderNum(con);
            listForClientDTO.setAvailableBalance(balance.getAvailableBalance());
            AvailableBalance availableBalance = storedStreamDalMapper.queryAvailableBalance(storedStreamListForClient.getId());
            if (availableBalance != null) {
                listForClientDTO.setAvailableBalance(availableBalance.getAvailableBalance());
            }
            if (storedStreamListForClient.getType() != null) {
                listForClientDTO.setTypeText(MbrStoredType.getStoredType(storedStreamListForClient.getType()).name);
            }
            if (storedStreamListForClient.getPayTerminal() != null) {
                listForClientDTO.setPayTerminalText(PayTerminal.getPayTerminalByCode(storedStreamListForClient.getPayTerminal()).name);
            }
            if (storedStreamListForClient.getAvailableBalance() != null) {
                listForClientDTO.setAvailableBalance(storedStreamListForClient.getAvailableBalance().setScale(2));
            }
            if (storedStreamListForClient.getGiftAmount() != null) {
                listForClientDTO.setGiftAmount(storedStreamListForClient.getGiftAmount().setScale(2));
            }
            if (storedStreamListForClient.getTransactionAmount() != null) {
                listForClientDTO.setTransactionAmount(storedStreamListForClient.getTransactionAmount().setScale(2));
            }
            detailListDTOs.add(listForClientDTO);
        });

        return new PagingResult<>(pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getTotal(), detailListDTOs);
    }

    /**
     * 当前会员储值明细列表--客户端
     *
     * @param condition
     * @return
     */
    public PagingResult<ClientStoredStreamListDTO> queryClientStoredStreamList(QueryClientStoredListCondition condition) {

        Assert.notNull(condition.getMemberId(), "会员id不能为空");
        Assert.notNull(condition.getMerchantId(), "商户id不能为空");
        RowBounds rowBounds = new RowBounds(condition.offset(), condition.limit());
        List<ClientStoredStreamList> list = storedStreamDalMapper.queryClientStoredStreamList(condition, rowBounds);

        PageInfo<ClientStoredStreamList> pageInfo = new PageInfo<>(list);

        List<ClientStoredStreamListDTO> dtoList = new ArrayList<>();

        list.forEach(clientStoredStreamList -> {
            ClientStoredStreamListDTO dto = new ClientStoredStreamListDTO(
                    clientStoredStreamList.getId(),
                    clientStoredStreamList.getStoredNum(),
                    clientStoredStreamList.getDateTime(),
                    clientStoredStreamList.getMemberCardNum(),
                    clientStoredStreamList.getMobile(),
                    clientStoredStreamList.getType(),
                    clientStoredStreamList.getTransactionAmount(),
                    clientStoredStreamList.getGiftAmount(),
                    clientStoredStreamList.getAvailableBalance(),
                    clientStoredStreamList.getPayTerminal()
            );
            //交易类型转换
            if (clientStoredStreamList.getType() != null) {
                dto.setTypeText(MbrStoredType.getStoredType(clientStoredStreamList.getType()).name);
            }
            if (clientStoredStreamList.getPayTerminal() != null) {
                dto.setPayTerminalText(MbrSourceTerminal.getSourceTerminal(clientStoredStreamList.getPayTerminal()).name);
            }
            if (StringUtils.isBlank(clientStoredStreamList.getStoreUserName())) {
                switch (clientStoredStreamList.getPayTerminal()) {
                    case 1:
                        dto.setStoreUserName("商户主账号");
                        break;
                    default:
                        dto.setStoreUserName("商户主账户");
                        break;
                }
            } else {
                dto.setStoreUserName(clientStoredStreamList.getStoreUserName());
            }
            dtoList.add(dto);
        });
        return new PagingResult<>(pageInfo.getPageSize(), pageInfo.getPageNum(),
                pageInfo.getTotal(), dtoList);
    }

    /**
     * 根据id查询储值明细详情-客户端
     *
     * @param id 储值明细id
     * @return
     */
    public StoredStreamInfoForClientDTO searchStoredStreamInfoForClient(Long id) {
        Assert.notNull(id, "储值明细id不能为空");
        StoredStreamInfoForClient storedStreamInfoForClient = storedStreamDalMapper.queryStoredStreamInfoForClient(id);
        StoredStreamInfoForClientDTO storedStreamInfoForClientDTO = new StoredStreamInfoForClientDTO();

        //对象转换
        CJBeanUtils.convertBean(storedStreamInfoForClient, storedStreamInfoForClientDTO, (source, target) -> {
            if (source.getType() != null) {
                target.setTypeText(MbrStoredType.getStoredType(source.getType()).name);
            }
            if (source.getPayType() != null) {
                target.setPayTypeText(PayEntry.getPayEntry((int) source.getPayType()).name);
            }
            if (source.getPayTerminal() != null) {
                target.setPayTerminalText(PayTerminal.getPayTerminalByCode(source.getPayTerminal()).name);
            }
            if (source.getStoreUser() == null) {
                target.setStoreUser(source.getMerchantUser());
                target.setStoreName(source.getMerchantName());
            }
        });
        return storedStreamInfoForClientDTO;
    }

}
